﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;

public partial class UserControls_team_view_seconds : FBasePage
{
    // Fields
    public NameValueCollection _system = new NameValueCollection();
    private Maticsoft.BLL.Ask askoper = new Maticsoft.BLL.Ask();
    protected List<Maticsoft.Model.Ask> asks = new List<Maticsoft.Model.Ask>();

    protected string buyUrl = string.Empty;
    protected Maticsoft.Model.Team CurrentTeam = null;
    protected long curtimefix = 0L;
    private string detail = "";
    protected long difftimefix = 0L;
    protected int imageindex = 2;
    protected int offset = 0;
    protected NameValueCollection order = new NameValueCollection();
    public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
    public List<Maticsoft.Model.Order> orderlist = new List<Maticsoft.Model.Order>();
    protected bool over = false;
    protected DateTime overtime = DateTime.Now;
    protected Maticsoft.Model.Partner part = new Maticsoft.Model.Partner();
    protected NameValueCollection partner = new NameValueCollection();
    protected int size = 0;
    protected TeamState state = TeamState.none;
    public string strtitle = "";
    public SystemConfig sysmodel = new SystemConfig();
    protected NameValueCollection team = new NameValueCollection();
    protected int teamid = 0;
    protected string teamstatestring = string.Empty;
    protected int twoline = 0;
    protected NameValueCollection user = new NameValueCollection();
    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
    public Maticsoft.Model.UserInfo usermodel = new Maticsoft.Model.UserInfo();
    private string where = "1=1";

    // Methods
    public string ListAsk()
    {
        StringBuilder builder = new StringBuilder();
        string wherestr = "comment is not null";
        if (base.ASSystem.Teamask == 0)
        {
            wherestr = wherestr + " and team_id=" + this.CurrentTeam.Id;
        }
        Pagers pagers = DbHelperSQL.SelectByPager("*", wherestr, "Create_time desc", 1, 15, "ask", string.Empty, string.Empty);
        this.asks = this.askoper.DataTableToList(pagers.MyData);
        for (int i = 0; i < pagers.MyData.Rows.Count; i++)
        {
            DataRow row = pagers.MyData.Rows[i];
            if ((row["Comment"] != null) && (row["Comment"].ToString() != ""))
            {
                builder.Append("<li id=\"ask-entry-" + row["id"].ToString() + "\" >");
                builder.Append("<div class=\"item\">");
                Maticsoft.Model.UserInfo model = new Maticsoft.BLL.UserInfo().GetModel(int.Parse(row["User_id"].ToString()));
                builder.Append("<p class=\"user\"><strong>" + model.Username + "</strong><span>" + DateTime.Parse(row["Create_time"].ToString()).ToString("yy-MM-dd") + "</span></p>");
                builder.Append("<div class=\"clear\"></div>");
                builder.Append("<p class=\"text\">" + row["Content"].ToString() + "</p>");
                builder.Append("<p class=\"reply\"><strong>回复：</strong>" + row["Comment"] + "</p>");
                builder.Append("</div>");
                builder.Append("</li>");
            }
        }
        return builder.ToString();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this._system = this.sysmodel.GetSystem();
        this.teamid = Helper.GetInt(base.Request["id"], 0);
        if (this.teamid > 0)
        {
            this.CurrentTeam = new Maticsoft.BLL.Team().GetModel(this.teamid);
        }
        else
        {
            this.CurrentTeam = base.CurrentTeam;
        }
        if (this.CurrentTeam != null)
        {
            this.orderlist = this.orderbll.GetByUserid(this.userbll.Getuserid(base.UserName), this.CurrentTeam.Id);
            if (this.CurrentTeam.Buyonce == "Y")
            {
                if ((this.CurrentTeam.Now_number < this.CurrentTeam.Max_number) && (this.orderlist.Count > 0))
                {
                    this.SetError("您已经成功购买了本单产品，请勿重复购买，快去关注一下其他产品吧！");
                }
            }
            else if (((this.CurrentTeam.Buyonce == "N") && (this.CurrentTeam.Now_number < this.CurrentTeam.Max_number)) && ((this.orderlist.Count != 0) && (this.orderlist.Count >= this.CurrentTeam.Per_number)))
            {
                this.SetError("您购买本单产品的数量已经达到上限，快去关注一下其他产品吧！");
            }
        }
        if (this.CurrentTeam == null)
        {
            base.Response.End();
        }
        else
        {
            this.twoline = (base.ASSystem.teamwhole == 0) ? 1 : 0;
            Maticsoft.BLL.Partner partner = new Maticsoft.BLL.Partner();
            this.curtimefix = Helper.GetTimeFix(DateTime.Now) * 0x3e8L;
            this.difftimefix = Helper.GetTimeFix(this.CurrentTeam.End_time) * 0x3e8L;
            this.difftimefix -= this.curtimefix;
            this.part = partner.GetModel(this.CurrentTeam.Partner_id);
            this.partner = Helper.GetObjectProtery(this.part);
            this.state = base.GetState(this.CurrentTeam);
            if ((this.state == TeamState.fail) || (this.state == TeamState.successtimeover))
            {
                this.over = true;
                if (this.CurrentTeam.Close_time.HasValue)
                {
                    this.overtime = this.CurrentTeam.Close_time.Value;
                }
                else
                {
                    this.overtime = this.CurrentTeam.End_time;
                }
            }
            if (this.state == TeamState.fail)
            {
                this.teamstatestring = "failure";
            }
            else if ((this.state == TeamState.successbuy) || (this.state == TeamState.successtimeover))
            {
                this.teamstatestring = "success";
            }
            else if (this.state == TeamState.successnobuy)
            {
                this.teamstatestring = "soldout";
            }
            if (this.CurrentTeam.Team_type == "seconds")
            {
                this.strtitle = "今日秒杀";
            }
            if (this.CurrentTeam.Team_type == "goods")
            {
                this.strtitle = "今日热销";
            }
            if (this.CurrentTeam.Team_type == "normal")
            {
                this.strtitle = "今日团购";
            }
            if (this.CurrentTeam.Begin_time > DateTime.Now)
            {
                this.buyUrl = "javascript: alert('秒杀还未开始...')";
            }
            else
            {
                this.buyUrl = "/team/buy.aspx?id=" + this.CurrentTeam.Id;
            }
            if (base.AsUser != null)
            {
                List<Maticsoft.Model.Order> modelList = new Maticsoft.BLL.Order().GetModelList(string.Concat(new object[] { "Team_id=", this.CurrentTeam.Id, " and User_id=", base.AsUser.Id, " and State='unpay'" }));
                if (modelList.Count > 0)
                {
                    this.order = Helper.GetObjectProtery(modelList[0]);
                }
            }
            this.team = Helper.GetObjectProtery(this.CurrentTeam);
            if (this.CurrentTeam.Min_number > 0)
            {
                this.size = Convert.ToInt32(Math.Ceiling(Convert.ToDouble((double)(190.0 * (Convert.ToDouble(this.CurrentTeam.Now_number) / Convert.ToDouble(this.CurrentTeam.Min_number))))));
                this.offset = Convert.ToInt32(Math.Ceiling(Convert.ToDouble((double)(5.0 * (Convert.ToDouble(this.CurrentTeam.Now_number) / Convert.ToDouble(this.CurrentTeam.Min_number))))));
            }
            this.blockflv1.UpdateView(this.CurrentTeam);
            this.blockask1.UpdateView(this.CurrentTeam, true);
            this.blockshare1.UpdateView(this.CurrentTeam);
        }
    }

}